package com.pingan.hive.udf;

import java.net.URLDecoder;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;

public class GetWordFromSearchStr extends UDF {

	public String evaluate(String url) throws Exception {
		return getWord(url);
	}

	public String getWord(String url) throws Exception {
		if (url == null || "".equals(url)) {
			return "";
		} else {
			if (StringUtils.contains(url.toLowerCase(), "soso.com/q?")) {
				String decodedWord = parseSearchWord(url.toLowerCase(), "w=");
				if (decodedWord == null || "".equals(decodedWord)) {
					return "";
				} else {
					return URLDecoder.decode(decodedWord, "gbk");
				}
			} else if (StringUtils.contains(url.toLowerCase(), "baidu.com/s?")) {
				String decodedWord = parseSearchWord(url.toLowerCase(), "wd=");
				if (decodedWord == null || "".equals(decodedWord)) {
					return "";
				} else {
					return URLDecoder.decode(decodedWord, "utf-8");
				}
			} else if (StringUtils
					.contains(url.toLowerCase(), "sogou.com/web?")) {
				String decodedWord = parseSearchWord(url.toLowerCase(),
						"query=");
				if (decodedWord == null || "".equals(decodedWord)) {
					return "";
				} else {
					return URLDecoder.decode(decodedWord, "utf-8");
				}
			} else if (StringUtils.contains(url.toLowerCase(), "so.com/s?")) {
				String decodedWord = parseSearchWord(url.toLowerCase(), "q=");
				if (decodedWord == null || "".equals(decodedWord)) {
					return "";
				} else {
					return URLDecoder.decode(decodedWord, "gbk");
				}
			} else if (StringUtils.contains(url.toLowerCase(), "yahoo.cn/s?")) {
				String decodedWord = parseSearchWord(url.toLowerCase(), "q=");
				if (decodedWord == null || "".equals(decodedWord)) {
					return "";
				} else {
					return URLDecoder.decode(decodedWord, "utf-8");
				}
			} else if (StringUtils.contains(url.toLowerCase(),
					"youdao.com/search?")) {
				String decodedWord = parseSearchWord(url.toLowerCase(), "q=");
				if (decodedWord == null || "".equals(decodedWord)) {
					return "";
				} else {
					return URLDecoder.decode(decodedWord, "utf-9");
				}
			} else if (StringUtils.contains(url.toLowerCase(), "google.com.hk")) {
				String decodedWord = parseSearchWord(url.toLowerCase(), "q=");
				if (decodedWord == null || "".equals(decodedWord)) {
					return "";
				} else {
					return URLDecoder.decode(decodedWord, "utf-8");
				}
			} else {
				return "";
			}
		}
	}

	public String parseSearchWord(String url, String name) {
		boolean isGoogle = StringUtils.contains(url.toLowerCase(),
				"google.com.hk");
		String[] array = null;
		if (isGoogle) {
			array = StringUtils.splitByWholeSeparatorPreserveAllTokens(url, "&"
					+ name);
		} else {
			array = StringUtils.splitByWholeSeparatorPreserveAllTokens(url,
					name);
		}

		if (array.length == 2) {
			String secondPar = array[1];
			String[] resultArray = StringUtils
					.splitByWholeSeparatorPreserveAllTokens(secondPar, "&");
			return resultArray[0];
		} else {
			return "";
		}

	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		GetWordFromSearchStr str = new GetWordFromSearchStr();
		System.out
				.println(str
						.evaluate("http://www.so.com/s?q=%CD%F8%D5%BE&ie=gbk&src=360portal&_re=0"));
	}

}
